/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 */

package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopProductInfoQuestion;
import com.hyyt.shopjsp.product.service.IShopProductInfoQuestionService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品答疑
 * @author Liu Ling on 2017/11/6 0006.
 */
@Controller
@RequestMapping("/back/productInfoQuestion")
public class ShopProductInfoQuestionController extends BaseController {

    private Logger log = Logger.getLogger(ShopProductInfoQuestionController.class);
    private String basePath = "/back/shop/product/shopProductQuestion/";

    @Resource
    private IShopProductInfoQuestionService shopProductInfoQuestionService;/**商品答疑service接口**/

    /**
     * 进入商品答疑页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoProductInfoQuestionPage")
    public String gotoProductInfoQuestionPage(){
        return basePath+"shopProductQuestionList";
    }

    /**
     * 商品答疑列表
     * @param customerName 会员名称
     * @param askBeginTime 咨询开始时间
     * @param askEndTime 咨询结束时间
     * @param askType 咨询类型
     * @param status 审核状态
     * @param page 分页
     * @param response 结果集
     */
    @RequestMapping(value = "listShopProductQuestion",method = RequestMethod.POST)
    @ResponseBody
    public void listShopProductQuestion(String customerName, String askBeginTime, String askEndTime, String askType, String status,Page page, HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(customerName)){
            params.put("customerName",customerName.trim());
        }
        if(Utils.objectIsNotEmpty(askBeginTime)){
            params.put("askBeginTime",askBeginTime);
        }
        if(Utils.objectIsNotEmpty(askEndTime)){
            params.put("askEndTime",askEndTime);
        }
        if(Utils.objectIsNotEmpty(askType)){
            params.put("askType",askType);
        }
        if(Utils.objectIsNotEmpty(status)){
            params.put("status",status);
        }
        int totalRecordCount = shopProductInfoQuestionService.countShopProductQuestionAndShopName(params);
        params.put("sortColumns","productInfoQuestionId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<Map<String, Object>> shopProductQuestionList = shopProductInfoQuestionService.queryShopProductQuestionAndShopName(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopProductQuestionList);
        ResultMsg.create(params,response);
    }

    /**
     * 根据商品答疑id获取集合
     * @param productInfoQuestionId 商品答疑id
     * @return 结果集
     */
    @RequestMapping(value = "getShopProductQuestionInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getShopProductQuestionInfo (Integer productInfoQuestionId){
        if (Utils.objectIsNotEmpty(productInfoQuestionId)){
            Map<String,Object> param = new HashMap<>();
            param.put("productInfoQuestionId",productInfoQuestionId);
            List<Map<String, Object>> list=shopProductInfoQuestionService.queryShopProductQuestionDetail(param);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(list));
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 审核商品答疑
     * @param productInfoQuestionId 商品答疑ID
     * @param status 审核状态
     * @return 返回布尔值
     */
    @RequestMapping(value = "auditProductQuestion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg auditProductQuestion(Integer productInfoQuestionId,Integer status) {
        if (Utils.objectIsNotEmpty(productInfoQuestionId)&&Utils.objectIsNotEmpty(status)) {
            ShopProductInfoQuestion shopProductInfoQuestion=shopProductInfoQuestionService.loadShopProductInfoQuestion(productInfoQuestionId);
            shopProductInfoQuestion.setStatus(status);
            BasicUsers basicUsers = getSessionUsers();
            shopProductInfoQuestion.setUserId(basicUsers.getUsersId());
            shopProductInfoQuestion.setUserName(basicUsers.getUserName());
            shopProductInfoQuestion.setCheckTime(new Timestamp(System.currentTimeMillis()));
            shopProductInfoQuestionService.updateShopProductInfoQuestion(shopProductInfoQuestion);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }
}
